package com.mobvel.zxstdzgl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mobclick.android.MobclickAgent;
import com.mobvel.zxstdzgl.ui.ArtListAdapter;
import com.mobvel.zxstdzgl.utils.AdTool;
import com.mobvel.zxstdzgl.utils.IndexHelper;

public class ArticleListActivity extends Activity implements OnItemClickListener {
	
	private ListView mArtList;
	private ArtListAdapter adapter;
	private List<Map<String, String>> data;
	private List<Map<String,String>> mDataList;
	private int mCateId;
	private int mArtNumStr;
	private RelativeLayout rlMore;
	
	private AdTool mAdTool;
	
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO Put your code here
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.art_list_view);
		mAdTool = new AdTool(this);
		
		data = new ArrayList<Map<String, String>>();
		data.clear();
		
		TextView tvTitle = (TextView)findViewById(R.id.tv_title);
		tvTitle.setText(R.string.app_name);
		
		mArtList = (ListView)findViewById(R.id.lv_articles);
		adapter = new ArtListAdapter(this);
		mDataList = setData();
		adapter.setData(mDataList);
		mArtList.setAdapter(adapter);
		mArtList.setOnItemClickListener(this);
		
		rlMore = (RelativeLayout)findViewById(R.id.rl_more);
		final TextView tvMore = (TextView)findViewById(R.id.tv_more);
		String offerSwitch = MobclickAgent.getConfigParams(this, "offer_switch");
		if(offerSwitch.equals("open")) {
			tvMore.setText(R.string.text_more);
			rlMore.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					mAdTool.showOffers();
				}
			});
		}else {
			tvMore.setText("返回");
			rlMore.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					finish();
				}
			});
		}
		
	}
	
	public void onResume() {
		super.onResume();
		
		MobclickAgent.onResume(this);
	}
	
	public void onPause() {
		super.onPause();
		
		MobclickAgent.onPause(this);
	}
	
	public void onDestroy() {
		
		super.onDestroy();
	}
	
	/**
	 * 设置ListView数据
	 */
	private List<Map<String,String>> setData() {
		List<String> artList = null;
		
		artList = IndexHelper.getAssetData(getResources(), 0);
		mCateId = 0;
		mArtNumStr = artList.size();
		
		for (int i = 0; i < artList.size(); i++) {
			Map<String, String> item = new HashMap<String, String>();
			item.put("ArticleName", artList.get(i));
			data.add(item);
		}
		
		return data;
	}
	
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		
		final Map<String, String> item=(Map<String, String>) mDataList.get(position);
		
		Intent i = new Intent(this, ArticleActivity.class);
		i.putExtra("CateId", mCateId);
		i.putExtra("ArticleNum", mArtNumStr);
		i.putExtra("ArticleName", item.get("ArticleName"));
		i.putExtra("ArticleIndex", position+1);
		startActivity(i);
	}
}
